package com.starling.extend.touch
{
	import com.starling.extend.display.BMovieClip;
	
	import flash.geom.Point;
	
	import starling.core.Starling;
	import starling.display.DisplayObject;
	import starling.display.DisplayObjectContainer;
	import starling.display.Quad;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	
	public class TouchObject
	{
		public static const TOUCH_BEGAN:String = "touchBegan";
		public static const TOUCH_ENDED:String = "touchEnded";
		public static const TOUCH_MOVED:String = "touchMoved";
		public static const TOUCH_TAP:String = "touchTap";
		
		private static var _quad:Quad;
		
		private var _callback:Function;
		private var _downStyle:String;
		
		private var _target:DisplayObject;
		private var _touch:Touch;
		private var _type:String;
		
		public function TouchObject(target:DisplayObject ,callback:Function ,downStyle:String)
		{
			_target = target;
			_callback = callback;
			_downStyle = downStyle;
			_target.addEventListener(TouchEvent.TOUCH ,touchHandler);
			
			if(downStyle==TouchObjectManage.ALPHA)
				_target.alpha = 0;
		}
		
		/** 返回函数 */
		internal function get callback():Function {return _callback;}
		
		/** 目标对象 */	
		public function get target():DisplayObject {return _target;}
		
		/** Touch */
		public function get touch():Touch {return _touch;}
		
		/** Touch事件类型 */
		public function get type():String {return _type;}
		
		
		/**
		 * 回收 
		 * 
		 */		
		internal function dispose():void
		{
			_down = false;
			_target.removeEventListener(TouchEvent.TOUCH ,touchHandler);
			_target = null;
			_callback = null;
		}
		
		
		/**
		 * 按下后的色块 
		 * @return 
		 * 
		 */		
		internal static function get quad():Quad
		{
			if(!_quad)
			{
				_quad = new Quad(2,2,0);
				_quad.alpha = 0.2;
				_quad.touchable = false;
			}
			return _quad;
		}
		
		
		/**
		 * 
		 * 
		 */		
		public function reset():void
		{
			_touchID = -1;
			_down = false;
		}
		
		
		
		
		/**---------------------------------
		 * 
		 *  功能区
		 * 
		 */ 
		private var _down:Boolean;
		private var _move:Boolean;
		private var _out:Boolean;
		private var _touchID:int = -1;
		
		private const _defaultPoint:Point = new Point();
		private const _startPoint:Point = new Point();
		private const _endPoint:Point = new Point();
		
		private function touchHandler(event:TouchEvent):void
		{
			/** 按下处理 */
			_touch = event.getTouch(_target, TouchPhase.BEGAN);
			
			if(_touch && _touchID==-1)
			{
				_touchID = _touch.id;
				_target.localToGlobal(_defaultPoint ,_startPoint);
				
				_type = TOUCH_BEGAN;
				setStatus();
				_callback(this);
				
				_down = true;
				_move = false;
				_out = false;
			}
			
			
			/** 是否按下 */
			if(!_down)return;
			
			
			
			/** 弹起处理 */
			_touch = event.getTouch(_target, TouchPhase.ENDED);
			if (_touch && _touch.id==_touchID)
			{
				_touchID = -1;
				_type = TOUCH_ENDED;
				setStatus();
				_callback(this);
				
				if(!_move && !_out) 
				{
					_type = TOUCH_TAP;
					_callback(this);
				}
				
				_down = false;
			}
			
			
			
			/** 移动处理 */
			_touch = event.getTouch(_target, TouchPhase.MOVED);
			if (_touch && _touch.id==_touchID)
			{
				if(_move==false)
				{
					_target.localToGlobal(_defaultPoint ,_endPoint);
					if(Point.distance(_startPoint ,_endPoint)>0)
						_move = true;
				}
				
				if(_out == false)
				{
					_out = _target.hitTest(_touch.getLocation(_target), true)==null;
				}
				
				_type = TOUCH_MOVED;
				if(_move || _out)setStatus();
				_callback(this);
			}
		}
		
		
		
		/** 设置状态 */
		private function setStatus():void
		{
			if(_downStyle!=null && _target!=Starling.current.stage)
			{
				if(_type==TOUCH_BEGAN)
				{
					if(_downStyle==TouchObjectManage.COLOR)
					{
						if(_target!=Starling.current.stage)
							_target.parent.addChild(TouchObject.quad);
						TouchObject.quad.width = _target.width;
						TouchObject.quad.height = _target.height;
						TouchObject.quad.x = _target.x;
						TouchObject.quad.y = _target.y;
					}
					
					if(_downStyle==TouchObjectManage.MOVIE)
					{
						if(_target is BMovieClip)
							(_target as BMovieClip).gotoAndStop(1);
					}
					
					if(_downStyle==TouchObjectManage.ALPHA)
					{
						_target.alpha = 1;
					}
				}else{
					if(_downStyle==TouchObjectManage.COLOR)
					{
						if(TouchObject.quad.parent!=null)
							TouchObject.quad.parent.removeChild(TouchObject.quad);
					}
					
					if(_downStyle==TouchObjectManage.MOVIE)
					{
						if(_target is BMovieClip)
							(_target as BMovieClip).gotoAndStop(0);
					}
					
					if(_downStyle==TouchObjectManage.ALPHA)
					{
						_target.alpha = 0;
					}
				}
			}
		}
	}
}